package com.example.administrator.youlu.manager;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.Contacts;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.youlu.R;
import com.example.administrator.youlu.been.Contact;
import com.example.administrator.youlu.utils.BitmapUtils;
import com.example.administrator.youlu.utils.ImageManager;

import butterknife.BindView;

public class DialogManager {




    public static void AddContactDialog(final Context mContext) {

        ImageView ivLeft, ivRight;
        final EditText etName, etPhone, etEmail, etLoading, etCompanyName;
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);

//        builder.setTitle("")
//                .setIcon(R.mipmap.ic_add_icon)
//                .setMessage("")
//                .create();

        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_layout, null);
        alertDialog.setContentView(view);
        ivLeft = view.findViewById(R.id.iv_Left);
        ivRight = view.findViewById(R.id.iv_right);
        etName = view.findViewById(R.id.etName);
        etPhone = view.findViewById(R.id.etPhone);
        etEmail = view.findViewById(R.id.etEmail);
        etLoading = view.findViewById(R.id.etLoading);
        etCompanyName = view.findViewById(R.id.etCompanyName);
        ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.dismiss();
            }
        });
        ivRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String name = etName.getText().toString();
                String phone = etPhone.getText().toString();
                String email = etEmail.getText().toString();
                String adress = etLoading.getText().toString();
                String companyName = etCompanyName.getText().toString();

                if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone)) {

                    Toast.makeText(mContext, "姓名和手机号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }

                Intent intent = new Intent();
                intent.setAction(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT);


                Uri uri = Uri.parse("tel:" + phone);
                intent.setData(uri);
                intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
                intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
                intent.putExtra(ContactsContract.Intents.Insert.COMPANY, companyName);
                intent.putExtra(ContactsContract.Intents.Insert.POSTAL, adress);
                mContext.startActivity(intent);
                alertDialog.dismiss();


            }
        });


    }

    public static void upDateContactDialog(final Context mContext, final Contact contact) {

        ImageView ivLeft, ivRight;
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        TextView etName,tvPhone,tvEmail,tvAdress;
        final ImageView IvIcon;

        final AlertDialog alertDialog = builder.create();
        alertDialog.show();
        View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_layout_update, null);
        alertDialog.setContentView(view);
        ivLeft = view.findViewById(R.id.iv_Left);
        ivRight = view.findViewById(R.id.iv_right);
        etName = view.findViewById(R.id.etName);
        tvPhone = view.findViewById(R.id.tvPhone);
        tvEmail = view.findViewById(R.id.tvEmail);
        tvAdress = view.findViewById(R.id.tvAdress);
        IvIcon = view.findViewById(R.id.IvIcon);
        ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_EDIT);//Uri.parse("content://com.android.contacts/contacts/"+cb.getContactId())
                intent.setData(Uri.parse("content://com.android.contacts/contacts/"+contact.get_id()));
                intent.putExtra("finishActivityOnSaveCompleted",true);
                mContext.startActivity(intent);
                alertDialog.dismiss();
            }
        });
        ivRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                alertDialog.dismiss();


            }
        });
        etName.setText(contact.getName());
        tvPhone.setText(contact.getPhone());
        tvEmail.setText(contact.getEmail());
        tvAdress.setText(contact.getAdress());
        Bitmap bitmap;
        if (contact.getPhoto()!=null){
             bitmap=ImageManager.formatBitmap(mContext,contact.getPhoto());
        }else {
            Bitmap bitmap1=BitmapUtils.getDrawBleOrBitmap(mContext,R.mipmap.ic_launcher);
            bitmap=ImageManager.formatBitmap(mContext,bitmap1);
        }

        IvIcon.setImageBitmap(bitmap);


    }

}
